/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.common.interceptor;


import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.encode.Base64;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户session拦截处理，后台session拦截，使用shiro拦截时，注释该方法
 *
 * @author ankang
 * @data on 2016/8/16 0016.
 */
public class UsersSessionInterceptor extends HandlerInterceptorAdapter {

    private static Logger logger = Logger.getLogger(UsersSessionInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            BasicUsers users = (BasicUsers) request.getSession().getAttribute(StringStaticKey.SESSION_USERS);//获取session
            String basePath = RedisDataReadUtil.getSysConfig(StringStaticKey.BASE_PATH_BACK);                               //获取项目路径
            if (Utils.objectIsEmpty(users)) {
                String url = String.valueOf(request.getRequestURL());                                                   //获取项目访问全路径
                String para = request.getQueryString();                                                                 //获取项目访问参数
                if (Utils.stringIsNotEmpty(para)) {                                                                     //处理请求的路径
                    url += "?" + para;
                }
                String jumpUrl = basePath + "/backLogin/index.htm";                                                           //跳转路径
                if (Utils.stringIsNotEmpty(jumpUrl)) {
                    jumpUrl += "?directUrl=" + Base64.encryptBASE64(url);                                          //加密处理
                }
                response.sendRedirect(jumpUrl);                                                                         //跳转登录页面
                return false;
            }
            return true;
        } else {
            return super.preHandle(request, response, handler);
        }
    }
}
